package com.proton.carepatchtemp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.adapter.ProfileAdapter;
import com.proton.carepatchtemp.base.BaseActivity;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.database.daomanager.ProfileManager;
import com.proton.carepatchtemp.database.table.Profile;
import com.proton.carepatchtemp.utils.NetUtils;
import com.proton.carepatchtemp.utils.TastyUtils;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;
import com.proton.carepatchtemp.utils.net.PrfCenter;
import com.sdsmdg.tastytoast.TastyToast;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

public class ProfileManagerActivity extends BaseActivity implements BGARefreshLayout.BGARefreshLayoutDelegate {

    ImageButton mBtnBack;
    TextView mTvAddProfile;
    RecyclerView mRcvProfileList;
    BGARefreshLayout mBgaRflProfile;

    private BGANormalRefreshViewHolder mBGANormalRefreshViewHolder;
    private LinearLayoutManager mLinearLayoutManager;

    private List<Profile> mProfiles = new ArrayList<>();
    private ProfileAdapter mAdapter;
    private LinearLayout mLnlPrfNullBitmap;

    @Override
    public void dealEvent(MainEvent event) {
        switch (event.getId()) {
            case Codes.NET_PRF_GETLIST:
                if (event.isSuccess()) {
                    mProfiles.clear();
                    mProfiles.addAll(ProfileManager.getAll());
                    mRcvProfileList.setVisibility(View.VISIBLE);
                    mAdapter.notifyDataSetChanged();
                    if (mProfiles.size() > 0) {
                        mLnlPrfNullBitmap.setVisibility(View.INVISIBLE);
                    } else {
                        mLnlPrfNullBitmap.setVisibility(View.VISIBLE);
                    }
                } else {
                    TastyUtils.makeTextShort(R.string.have_no_more_prf, TastyToast.INFO);
                }
                mBgaRflProfile.endRefreshing();
                break;
            case Codes.PRF_DELETE:
                if (event.isSuccess()) {
                    TastyUtils.makeTextShort(R.string.prf_delete_success, TastyToast.SUCCESS);
                    if (mProfiles.size() <= 0) {
                        mBgaRflProfile.beginRefreshing();
                    }
                } else {
                    TastyUtils.makeTextShort(getString(R.string.prf_delete_failed) + event.getMsg(), TastyToast.ERROR);
                }

                if (mProfiles.size() <= 0) {
                    PrfCenter.getlist();
                }

                break;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_manager);
        initView();
        mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRcvProfileList.setLayoutManager(mLinearLayoutManager);
        mRcvProfileList.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mAdapter = new ProfileAdapter(this, mProfiles);
        mRcvProfileList.setAdapter(mAdapter);

        setListener();
        setBgaRefreshLayout();
        if (NetUtils.noNet()) {
            TastyUtils.makeTextShort(R.string.have_no_netconnect, TastyToast.CONFUSING);
            finish();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        mBgaRflProfile.beginRefreshing();
    }

    @Override
    public void setListener() {
        mBtnBack.setOnClickListener(view -> {
            finish();
        });

        mTvAddProfile.setOnClickListener(view -> {
            Intent goAdd = new Intent(ProfileManagerActivity.this, AddProfileActivity.class);
            startActivity(goAdd);
        });

    }

    private void setBgaRefreshLayout() {
        mBGANormalRefreshViewHolder = new BGANormalRefreshViewHolder(this, true);
        mBGANormalRefreshViewHolder.setLoadingMoreText(getString(R.string.loading));
        mBGANormalRefreshViewHolder.setPullDownRefreshText(getString(R.string.string_pull_2_refresh));
        mBGANormalRefreshViewHolder.setRefreshingText(getString(R.string.loading));
        mBGANormalRefreshViewHolder.setReleaseRefreshText(getString(R.string.string_release_text));
        mBgaRflProfile.setRefreshViewHolder(mBGANormalRefreshViewHolder);
        mBgaRflProfile.setDelegate(this);
        mBgaRflProfile.setIsShowLoadingMoreView(true);
    }

    @Override
    public void onEventMainThread(MainEvent event) {
        dealEvent(event);
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        if (NetUtils.noNet()) {
            mBgaRflProfile.endRefreshing();
            TastyUtils.makeTextShort(R.string.have_no_netconnect, TastyToast.CONFUSING);
            mLnlPrfNullBitmap.setVisibility(View.VISIBLE);
            mProfiles.clear();
            mAdapter.notifyDataSetChanged();
            return;
        }
        mProfiles.clear();
        PrfCenter.getlist();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        if (NetUtils.noNet()) {
            mBgaRflProfile.endRefreshing();
            mBgaRflProfile.endLoadingMore();
            TastyUtils.makeTextShort(R.string.have_no_netconnect, TastyToast.CONFUSING);
            return false;
        }
        TastyUtils.makeTextShort(R.string.have_no_more, TastyToast.CONFUSING);
        mBgaRflProfile.endLoadingMore();
        return false;
    }

    private void initView() {
        mBtnBack = findViewById(R.id.btn_back);
        mTvAddProfile = findViewById(R.id.tv_add_profile);
        mRcvProfileList = findViewById(R.id.rcv_profile_list);
        mBgaRflProfile = findViewById(R.id.bga_rfl_profile);
//        mIvBitmapNullPrf = (ImageView) findViewById(R.id.iv_bitmap_null_prf);
        mLnlPrfNullBitmap = findViewById(R.id.lnl_prf_null_bitmap);
    }
}
